//
//  AppDelegate.swift
//  HuaDaoPeifu
//
//  Created by Erickson on 16/3/28.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD
import SnapKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        IQKeyboardManager.shared.enable = true
        
        HDAccountHandler.readUserInfo()
        
        showGuideView()
        
        startUMAnalytics()
        
        umShare()
        
        // JSPatch 接入
//        JPEngine.start()
//        JPEngine.evaluateScript(script)
        
//        /**
//         
//         推送相关 startSdkWithAppId 必须在主线程里
//         */
//        GeTuiSdk.startSdkWithAppId(GTConstant.appId, appKey: GTConstant.appKey, appSecret: GTConstant.appSecret, delegate: self)
//        
//        registerUserNotification(application)
        
        
        
        return true
    }

    
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        showbadgeValue()
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        let result:Bool = UMSocialManager.default().handleOpen(url, options: options)
        if result == false {

        }
        return result
    }
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        let result:Bool = UMSocialManager.default().handleOpen(url)
        if result == false {

        }
        return result
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}
extension AppDelegate {
    
    func startUMAnalytics() {
//        let config = UMAnalyticsConfig()
//        config.appKey = UMAppKey
//        MobClick.start(withConfigure: config)
    }
    
    // 分享相关
    func umShare(){
        UMConfigure.setLogEnabled(true)
        UMConfigure.initWithAppkey(UMAppKey, channel: "apple")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: WXAppID, appSecret: WXAppSecret, redirectURL:"http://www.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatTimeLine, appKey: WXAppID, appSecret: WXAppSecret, redirectURL:"http://www.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: QQAppID, appSecret: QQAppKey, redirectURL:"http://www.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.qzone, appKey: QQAppID, appSecret: QQAppKey, redirectURL:"http://www.umeng.com/social")
    }
    
    func showGuideView() {
        
/*  现在的策略是，只要显示过引导页，在不删除应用的情况下，更新也不显示，主要是考虑到第五个登录注册引导页问题
         */
        let userDefaults = UserDefaults.standard
        let haveShow = userDefaults.string(forKey: "haveShowGuide")
        if haveShow == nil || haveShow == "" {
            userDefaults.setValue("haveShowGuide", forKey: "haveShowGuide")
            let guideVC = GuideViewController()
            self.window?.rootViewController =  guideVC
        }
    }
    
    
    func showbadgeValue() {
        
        
        
        if HDAccountHandler.loginType == .notLogin {
            return
        }
        HDApiManagerHandler.requestCaseNumber({ (response) in
            if response["status"]?.int32Value == 1 {
                let data = response["data"] as! NSDictionary
                
                let caseValue = (data["case"]! as AnyObject).int32Value
                
                let archiveValue = (data["archives"]! as AnyObject).int32Value
                
                let tabbarController = UIApplication.shared.keyWindow?.rootViewController as! UITabBarController
                
                if caseValue! > 0 {
                    tabbarController.tabBar.showBadgeValueAtItemIndex(0)
                }
                if archiveValue! > 0 {
                    tabbarController.tabBar.showBadgeValueAtItemIndex(1)
                }
            

            }
            print(response)
            }) { (error) in
                
        }
    }
}


